package Logic;

import Graphics.FieldGraphics;
import HelperClasses.Constants;
import HelperClasses.Vektor;
import Player.Player;
import java.awt.Color;
import java.awt.Point;

/**
 *
 * @author johannes
 */
public class Stone {

    private Vektor pos;
    private Vektor impuls;
    public Player owner;
    private boolean isMoving = true;
    private boolean isInGame = true;
    private Vektor nullVector = new Vektor(0, 0);
    private double curling = 0;

    public Stone(int xpos, int ypos, Player owner) {
        this.pos = new Vektor(xpos, ypos);
        this.impuls = nullVector;
        this.owner = owner;
    }

    public Stone(int xpos, int ypos, Vektor impuls, Player owner) {
        this.pos = new Vektor(xpos, ypos);
        this.impuls = impuls;
        this.owner = owner;
    }

    public Stone(Point pos, Vektor impuls, Player owner) {
        this.pos = new Vektor(pos.x, pos.y);
        this.impuls = impuls;
        this.owner = owner;
    }

    public Stone(Point pos, Player owner) {
        this.pos = new Vektor(pos.x, pos.y);
        this.impuls = nullVector;
        this.owner = owner;
    }

    public Color getColor() {
        return owner.getColor();
    }

    public Vektor getImpuls() {
        return impuls;
    }

    public void setImpuls(Vektor impuls) {
        if (impuls == null) {
            this.impuls = new Vektor(0.0, 0.0);
        }
        this.impuls = impuls;
    }

    public Player getOwner() {
        return owner;
    }

    public void setOwner(Player owner) {
        this.owner = owner;
    }

    public Point getPos() {
        return new Point(pos.getXInt(), pos.getYInt());
    }

    public Vektor getPosVek() {
        return pos;
    }

    public void setPos(Point pos) {
        this.pos = new Vektor(pos.x, pos.y);
    }

    public void setPos(int x, int y) {
        this.pos = new Vektor(x, y);
    }

    public void setPos(double x, double y) {
        this.pos = new Vektor(x, y);
    }

    public double getCurling() {
        return curling;
    }

    public void setCurling(double curling) {
        this.curling = curling;
    }

    public void move() {
//        if (pos.getX() <= 8.0 || pos.getX() >= 92.0) {
//            impuls.setX(-impuls.getX() * 0.990);
//        }
        Vektor newPos = new Vektor();
        // Impuls verwenden
        newPos.setX(pos.getX() + impuls.getX() * Constants.impulseFactor);
        newPos.setY(pos.getY() + impuls.getY() * Constants.impulseFactor);
        // Curling!
        newPos.setX(newPos.getX() + (impuls.getX() * Constants.curlingSpeedFactor + curling * Constants.curlingFactor));  
        // Untergrund!!

        this.pos = newPos;
    }

    // true, wenn noch bewegt, false sonst
    public boolean slowdown() {
        impuls.setX(impuls.getX() * Constants.slowdownFactor);
//        System.out.println(impuls.getY());
        impuls.setY(impuls.getY() * Constants.slowdownFactor);
//        System.out.println(impuls.getY());
//        System.out.println(pos.getX());
        if (getImpuls().getX() <= Constants.minimumImpulseLeft && getImpuls().getY() >= -Constants.minimumImpulseLeft) {
            setIsMoving(false);
            setImpuls(new Vektor(0, 0));
            return false;
        }
        return true;
    }

    public boolean isInGame() {
        return isInGame;
    }

    public void setIsInGame(boolean isInGame) {
        this.isInGame = isInGame;
        this.isMoving = false;
        if (isInGame == false) {
            // in die Ecke (rot links, gelb rechts)
            System.out.println(getColor().toString());
            if (getColor().equals(Color.RED)) {
                setPos(FieldGraphics.stoneSize / 2, FieldGraphics.stoneSize / 2);
            } else {
                setPos(FieldGraphics.internalWidth - (FieldGraphics.stoneSize / 2), FieldGraphics.stoneSize / 2);
            }
            setImpuls(nullVector);
        }
    }

    public boolean isMoving() {
        return isMoving;
    }

    public void setIsMoving(boolean isMoving) {
        this.isMoving = isMoving;
    }

    public boolean checkOutOfBounds() {
//        System.out.println((pos.getY() + FieldGraphics.stoneSize / 2));
//        System.out.println((FieldGraphics.houseCenter.y + FieldGraphics.hogLinePos));
        if ((pos.getX() < FieldGraphics.stoneSize / 2) || (pos.getY() - 40 < -FieldGraphics.stoneSize / 2) ||
                (pos.getX() + FieldGraphics.stoneSize / 2 > FieldGraphics.internalWidth) ||
                ((pos.getY() - FieldGraphics.stoneSize / 2 > FieldGraphics.houseCenter.y + FieldGraphics.hogLinePos) && (!isMoving))) {
            // Aus dem Spiel nehmen
            System.out.println("kicking stone");
            setIsInGame(false);
            return true;
        }
        return false;
    }
}
